/**
 * @Project RSLsXWG
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate May 2, 2011
 * @Createtime 11:43:21 PM
 */

package com.gigalaxy.game.card
{
	import com.gigalaxy.game.card.events.CardEvent;
	import com.gigalaxy.game.card.events.CardRenderEvent;
	import com.gigalaxy.tweens.GGMax;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;

	public class CardRenderer extends Sprite
	{
		/**
		 * lưng card
		 */
		public var _cardBack:CardBackAAA;

		/**
		 * mặt card
		 */
		public var _cardFace:CardFaceAAA;

		/**
		 * card dto
		 */
		private var _card:Card;

		/**
		 * @private
		 * see description in get / set
		 */
		private var _interactable:Boolean;

		/**
		 * is card on hand or threw<br />
		 */
		public var onHand:Boolean;

		/**
		 * is card selected to do something<br />
		 * <b>EG:</b><br />
		 * throw to table<br />
		 */
		public var selected:Boolean;

		/**
		 * @private
		 * see description in get / set
		 */
		private var _revealed:Boolean;

		private var _glowFilter:GlowFilter;
		private var _outerGlowFilter:GlowFilter;

		/**
		 * @private
		 * see description in get / set
		 */
		private var _highlight:uint;

		/**
		 * nhấp nháy bài
		 */
		private var _blink:uint;

		/**
		 * chứa thông tin thay đổi 'visual' của card<br />
		 * eg: tọa độ x,y. góc quay...<br />
		 * note: vso - visual object
		 */
		public var cardVSO:CardRendererVDO;

		public function CardRenderer(card:Card = null)
		{
			super();

			cardVSO = new CardRendererVDO();

			visible = false; // only visible when on hand

			_card = card;
			_card.addEventListener(CardEvent.CARD_VALUE_CHANGE, cardValueChangeHandler);

			_cardFace = new CardFaceAAA();

			//*
			_cardFace.x = -_cardFace.width / 2;
			_cardFace.y = -_cardFace.height / 2;
			//*/

			/*
			_cardFace.x = -54;
			_cardFace.y = -75;
			//*/

			addChild(_cardFace);

			_cardBack = new CardBackAAA();

			//*
			_cardBack.x = -_cardBack.width / 2;
			_cardBack.y = -_cardBack.height / 2;
			//*/

			/*
			_cardBack.x = -54;
			_cardBack.y = -75;
			//*/

			addChild(_cardBack);

			_glowFilter = new GlowFilter(0x00dd00, .9, 4, 4, 8, 1);
			_outerGlowFilter = new GlowFilter(0x00dd00, .9, 8, 8, 4, 1);

			highlight = 0;
		}

		public function resetData():void
		{
			_card.resetData();

			stopDrag(); // ^^

			visible = false;
			alpha = 1;
			x = 0;
			y = 0;
			rotation = 0;

			onHand = false;
			selected = false;
			interactable = false;
			highlight = 0;
			blink = 0;
		}

		public function get card():Card
		{
			return _card;
		}

		public function set card(value:Card):void
		{
			_card = value;
		}

		public function cardValueChangeHandler(event:CardEvent):void
		{
			var value:int;
			var suit:int;

			switch (_card.value)
			{
				case Card.ACE:
					value = 1;
					break;
				case "2":
				case "3":
				case "4":
				case "5":
				case "6":
				case "7":
				case "8":
				case "9":
				case "10":
					value = int(_card.value);
					break;
				case Card.JACK:
					value = 11;
					break;
				case Card.QUEEN:
					value = 12;
					break;
				case Card.KING:
					value = 13;
					break;
				default:
					value = 0;
					break;
			}

			switch (_card.suit)
			{
				case Card.CLUBS:
					suit = 1;
					break;
				case Card.DIAMONDS:
					suit = 2;
					break;
				case Card.HEARTS:
					suit = 3;
					break;
				case Card.SPADES:
					suit = 4;
					break;
				default:
					suit = 0;
					break;
			}

			var des:int = value + (suit - 1) * 13;
			_cardFace.gotoAndStop(des);
		}

		public function toggleCardSelection():void
		{
			if (selected)
			{
				selected = false;
			}
			else
			{
				selected = true;
			}
		}

		/**
		 * is card interactable?<br />
		 * <b>EG:</b><br />
		 * card can be select, drag,...<br />
		 */
		public function get interactable():Boolean
		{
			return _interactable;
		}

		public function set interactable(value:Boolean):void
		{
			_interactable = value;

			if (_interactable)
			{
				useHandCursor = true;
				buttonMode = true;

				if (!hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
				}
			}
			else
			{
				useHandCursor = false;
				buttonMode = false;

				if (hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
				}
			}
		}

		protected function mouseDownHandler(event:MouseEvent):void
		{
			//this.startDrag();

			var e:CardRenderEvent = new CardRenderEvent(CardRenderEvent.MOUSE_DOWN, null, true, true);
			dispatchEvent(e);

			if (!hasEventListener(MouseEvent.MOUSE_UP))
			{
				addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);
			}
		}

		private function mouseUpHandler(event:MouseEvent):void
		{
			//this.stopDrag();

			var e:CardRenderEvent = new CardRenderEvent(CardRenderEvent.MOUSE_UP, null, true, true);
			dispatchEvent(e);

			if (hasEventListener(MouseEvent.MOUSE_UP))
			{
				removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			}
		}

		/**
		 * is card revealed to see it suit and value?<br />
		 */
		public function get revealed():Boolean
		{
			return _revealed;
		}

		public function set revealed(value:Boolean):void
		{
			_revealed = value;

			if (_revealed)
			{
				_cardBack.alpha = 0;
			}
			else
			{
				_cardBack.alpha = 1;
			}
		}

		/**
		 * kiểu highlight<br />
		 * 0: bỏ hightlight<br />
		 * 1: green - 0x00dd00<br />
		 * 2: orange - 0xff9900<br />
		 * 3: red - 0xee0000<br />
		 */
		public function get highlight():uint
		{
			return _highlight;
		}

		public function set highlight(value:uint):void
		{
			_highlight = value;

			switch (value)
			{
				case 0:
					_cardFace.filters = [];
					break;
				case 1:
					_glowFilter.color = 0x00dd00;
					_cardFace.filters = [_glowFilter]; // phải gán lại filter thì mới có hiệu lực???? khác so với filter trong count down >> có thể có sự khác biệt giữa spark và flash filter!!!
					break;
				case 2:
					_glowFilter.color = 0xff9900;
					_cardFace.filters = [_glowFilter];
					break;
				case 3:
					_glowFilter.color = 0xee0000;
					_cardFace.filters = [_glowFilter];
					break;
				case 4:
					_glowFilter.color = 0xffffff;
					_cardFace.filters = [_glowFilter];
					break;
				default:
					break;
			}
		}

		public function get blink():uint
		{
			return _blink;
		}

		public function set blink(value:uint):void
		{
			_blink = value;

			switch (value)
			{
				case 0:
					GGMax.killTweensOf(_cardFace, true);
					_cardFace.filters = [];
					break;
				case 1:
					//_outerGlowFilter.color = 0x00dd00;
					//_cardFace.filters = [_outerGlowFilter]; // phải gán lại filter thì mới có hiệu lực???? khác so với filter trong count down >> có thể có sự khác biệt giữa spark và flash filter!!!
					GGMax.to(_cardFace, .5, {glowFilter: {color: 0x00dd00, alpha: 1, blurX: 8, blurY: 8, strength: 4}, yoyo: true, repeat: -1});
					break;
				case 2:
					//_outerGlowFilter.color = 0xff9900;
					//_cardFace.filters = [_outerGlowFilter];
					GGMax.to(_cardFace, .5, {glowFilter: {color: 0xff9900, alpha: 1, blurX: 8, blurY: 8, strength: 4}, yoyo: true, repeat: -1});
					break;
				case 3:
					//_outerGlowFilter.color = 0xee0000;
					//_cardFace.filters = [_outerGlowFilter];
					GGMax.to(_cardFace, .5, {glowFilter: {color: 0xee0000, alpha: 1, blurX: 8, blurY: 8, strength: 4}, yoyo: true, repeat: -1});
					break;
				case 4:
					//_outerGlowFilter.color = 0xffffff;
					//_cardFace.filters = [_outerGlowFilter];
					GGMax.to(_cardFace, .5, {glowFilter: {color: 0xffffff, alpha: 1, blurX: 8, blurY: 8, strength: 4}, yoyo: true, repeat: -1});
					break;
				default:
					GGMax.killTweensOf(_cardFace, true);
					_cardFace.filters = [];
					break;
			}
		}
	}
}


